import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;


public class Turtle extends JFrame {
    static private Graphics2D offscreen;    
    static private Image offscreenPic;

    
    static private double x = 0.0, y = 384.0;
    static private double facing = 0.0;    // facing this many degrees counterclockwise
    static private Insets insets;               // border around JFrame that we shouldn't use
    static private int width = 1024;
    static private int height = 768 ;          // size of drawing area in pixels
    static private Color bg;                    // background color
    static private double stepSize = 10;
    static private double angle = LSystem.getAngle();
    static private double savedX, savedY, savedAngle;
    
    
    
    public Turtle(String axiom) {
        this.setSize(new Dimension(1024,768));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("L-System (rxh09u)");
        this.setVisible(true);
        init();        
        iterate(axiom);
    }
    
    private void iterate(String axiom) {
        char c;
        int length = axiom.length();
        
        for (int i = 0; i < length; i++)
        {
            c = axiom.charAt(i);
            System.out.println(c);
            performAction(c);
            
        }
    }
    
    private void performAction(char c)
    {
        if (c == 'A' || c == 'B' || c == 'F' || c == 'G') {
            c = 'F';
        }
        switch (c) {
            case 'F':    forward(stepSize);
            break;
            case '-':    rotate(angle);
            break;
            case '+':    rotate(angle * -1);
            break;
            default:
            break;
        }
    }
    
    
    
    private void init() {
        offscreenPic = createImage(width, height);
        offscreen = (Graphics2D) offscreenPic.getGraphics();
        
        offscreen.translate(0, height);
        offscreen.scale(1.0, -1.0);
        
    }
    public static void rotate(double angle) {
        System.out.println("Rotating " + angle + " degrees left.");
        savedAngle =
        facing += angle;
    }
    
    public static void teleportTo(double x, double y) {
        Turtle.x = x;
        Turtle.y = y;
    }
    
    
    public static void saveState() {
        
    }
    
    public static void loadState() {
    }
    
    
    
    public static void forward(double d) {
        double oldx = x;
        double oldy = y;
        x += d * Math.cos(Math.toRadians(facing));
        y += d * Math.sin(Math.toRadians(facing));
        offscreen.draw(new Line2D.Double(x, y, oldx, oldy));
    }
    
    
    
    public void paint(Graphics g) {
        if (offscreenPic != null && g != null)
        g.drawImage(offscreenPic, insets.left, insets.top, this);
    }
    
    
    
}
